#include "stdafx.h"
#include "NumpadWidget.h"

using namespace AirCrash::Calypso;

NumpadWidget::NumpadWidget(QWidget * parent) : QWidget(parent) {
}

NumpadWidget::~NumpadWidget() {
}

void NumpadWidget::paintEvent(QPaintEvent * e) {
	QPainter painter(this);
	painter.setRenderHint(QPainter::SmoothPixmapTransform);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setRenderHint(QPainter::HighQualityAntialiasing);

	painter.setOpacity(0.8);
	painter.fillRect(rect(), Qt::black);

	painter.setOpacity(1);
	QMapIterator< QString, QPair<QRect, QImage> > it(mKeys);
	while (it.hasNext()) {
		QPair<QRect, QImage> keyValue(it.next().value());
		painter.drawImage(keyValue.first, keyValue.second);
	}
}

void NumpadWidget::scale(float scale) {
	mKeys.clear();
	QString imagePath(Config::get("Numpad", "imagePath").toString()),
		imageExt(Config::get("Numpad", "imageExt").toString());
	mKeys.insert("0", qMakePair( scaleRect(Config::get("Numpad", "0").toRect(), scale), QImage(imagePath + "0" + imageExt) ));
	mKeys.insert("1", qMakePair( scaleRect(Config::get("Numpad", "1").toRect(), scale), QImage(imagePath + "1" + imageExt) ));
	mKeys.insert("2", qMakePair( scaleRect(Config::get("Numpad", "2").toRect(), scale), QImage(imagePath + "2" + imageExt) ));
	mKeys.insert("3", qMakePair( scaleRect(Config::get("Numpad", "3").toRect(), scale), QImage(imagePath + "3" + imageExt) ));
	mKeys.insert("4", qMakePair( scaleRect(Config::get("Numpad", "4").toRect(), scale), QImage(imagePath + "4" + imageExt) ));
	mKeys.insert("5", qMakePair( scaleRect(Config::get("Numpad", "5").toRect(), scale), QImage(imagePath + "5" + imageExt) ));
	mKeys.insert("6", qMakePair( scaleRect(Config::get("Numpad", "6").toRect(), scale), QImage(imagePath + "6" + imageExt) ));
	mKeys.insert("7", qMakePair( scaleRect(Config::get("Numpad", "7").toRect(), scale), QImage(imagePath + "7" + imageExt) ));
	mKeys.insert("8", qMakePair( scaleRect(Config::get("Numpad", "8").toRect(), scale), QImage(imagePath + "8" + imageExt) ));
	mKeys.insert("9", qMakePair( scaleRect(Config::get("Numpad", "9").toRect(), scale), QImage(imagePath + "9" + imageExt) ));
	mKeys.insert("delete", qMakePair( scaleRect(Config::get("Numpad", "delete").toRect(), scale), QImage(imagePath + "Delete" + imageExt) ));
	mKeys.insert("return", qMakePair( scaleRect(Config::get("Numpad", "return").toRect(), scale), QImage(imagePath + "Return" + imageExt) ));
}

void NumpadWidget::mousePressEvent(QMouseEvent * e) {
	QMapIterator< QString, QPair<QRect, QImage> > it(mKeys);
	while (it.hasNext()) {
		it.next();
		QString key(it.key());
		QPair<QRect, QImage> value(it.value());
		QPoint pressedPoint( this->mapFromGlobal(e->globalPos()) );
		if (value.first.contains(pressedPoint)) {
			mKeyDown = key;
		}
	}
}

void NumpadWidget::mouseReleaseEvent(QMouseEvent * e) {
	QMapIterator< QString, QPair<QRect, QImage> > it(mKeys);
	while (it.hasNext()) {
		it.next();
		QString key(it.key());
		QPair<QRect, QImage> value(it.value());
		QPoint pressedPoint( this->mapFromGlobal(e->globalPos()) );
		if (value.first.contains(pressedPoint) && mKeyDown == key) {
			if (key == "delete") {
				emit numpadDelete();
			} else if (key == "return") {
				emit numpadGetCode();
			} else {
				emit numpadInsert(key);
			}
		}
	}
	mKeyDown.clear();
}